package com.cryptoneedle.center.source.properties;


import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * <p>description: SSH配置项 </p>
 * <p>
 * 用于作为连接数据源的跳板机信息
 *
 * @author CryptoNeedle
 * @date 2025-06-23
 */
@Data
@Component
@ConfigurationProperties(prefix = "ssh")
@Schema(description = "SSH配置项")
public class SshProperties implements InitializingBean {
    
    @Schema(description = "是否开启")
    private Boolean enabled = false;
    
    @Schema(description = "主机地址")
    private String host;
    
    @Schema(description = "端口")
    private Integer port;
    
    @Schema(description = "用户名")
    private String user;
    
    @Schema(description = "密码")
    private String password;
    
    @Schema(description = "起始代理占用端口号")
    private Integer beginOpenPort = 28980;
    
    @Schema(description = "超时时间(ms)")
    private Integer timeout = 3 * 1000;
    
    
    @Override
    public void afterPropertiesSet() throws Exception {
        if (enabled) {
            if (StringUtils.isBlank(host)) {
                throw new IllegalStateException("[SSH] 配置 -> 错误 => 主机地址不能为空");
            }
            if (port == null || port <= 0 || port > 65535) {
                throw new IllegalStateException("[SSH] 配置 -> 错误 => 端口号必须在1-65535之间");
            }
            if (StringUtils.isBlank(user)) {
                throw new IllegalStateException("[SSH] 配置 -> 错误 => 用户名不能为空");
            }
            if (StringUtils.isBlank(password)) {
                throw new IllegalStateException("[SSH] 配置 -> 错误 => 密码不能为空");
            }
            if (beginOpenPort == null || beginOpenPort < 1024 || beginOpenPort > 65535) {
                throw new IllegalStateException("[SSH] 配置 -> 错误 => 起始代理端口号必须在1024-65535之间");
            }
        }
    }
}